package servlet.zyb;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import bean.zyb.Question;

public class CheckAllMessageServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			process(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void process(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException, Exception {

		req.setCharacterEncoding("UTF-8");
		SAXBuilder builder = new SAXBuilder();
		Document document = builder.build(new File(
				"E:\\MyeclipseWorkspace\\vote\\question.xml"));

		Element root = document.getRootElement();

		List<Element> list = root.getChildren();

		List<Question> questions = new ArrayList<Question>();
		for (int i = 0; i < list.size(); i++) {// 遍历question.xml里的内容

			Element question = list.get(i);
			String qName = question.getChild("questionname").getText();
			String qType = question.getChild("questiontype").getText();

			Question q = new Question(qName, qType);// 使用bean
			questions.add(q);
		}

		req.setAttribute("qusetions", questions);

		req.getRequestDispatcher("showMessage.jsp").forward(req, resp);// 转发出去
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			process(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
